package com.elm.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Many;
import com.elm.po.Authority;
import com.elm.po.Person;
import com.elm.po.User;

@Mapper
public interface UserMapper {
	@Select("select u.id, u.creator, u.is_deleted, u.updater, u.activated, u.username, u.status " +
	        "from users u join user_authority ua on u.id=ua.user_id " +
	        "where username=#{username} And password=#{password} LIMIT 1")
    @Results({
        @Result(column = "id", property = "id"),
        @Result(column = "creator", property = "creator"),
        @Result(column = "is_deleted", property = "isDeleted"),
        @Result(column = "updater", property = "updater"),
        @Result(column = "activated", property = "activated"),
        @Result(column = "password", property = "password"),
        @Result(column = "username", property = "username"),
        @Result(column = "status", property = "status"),
        // 关联查询用户权限列表
        @Result(column = "id", property = "authorities",
                many = @Many(select = "com.elm.mapper.UserAuthorityMapper.getAuthorityById"))
    })
    User getUserByNameByPass(String username, String password);

    @Select("select id from users where username=#{username}")
    int getUserIdByName(String username);

    @Insert("INSERT INTO users ( creator, updater, is_deleted, " +
            "activated, password, username) " +
            "VALUES (#{creator}, #{updater}, #{isDeleted}, " +
            "#{activated}, #{password}, #{username})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int createUser(User user);

    @Select("select id,  creator, is_deleted, updater, activated, username, status from users where id=#{id}")
    @Results({
        @Result(column = "id", property = "id"),
        @Result(column = "creator", property = "creator"),
        @Result(column = "is_deleted", property = "isDeleted"),
        @Result(column = "updater", property = "updater"),
        @Result(column = "activated", property = "activated"),
        @Result(column = "password", property = "password"),
        @Result(column = "username", property = "username"),
        @Result(column = "status", property = "status"),
        // 关联查询用户权限列表
        @Result(column = "id", property = "authorities",
                many = @Many(select = "com.elm.mapper.UserAuthorityMapper.getAuthorityById"))
    })
    User getUserById(int id);

    @Insert("Insert INTO business_user_identity(user_id,business_id,identity_type,remarks)"+
            "VALUES (#{staffUserId}, #{businessId}, 'staff', null) ")
    int addStaff(int staffUserId,Integer businessId);

    @Delete("Delete from business_user_identity where user_id=#{staffUserId} And business_id=#{businessId}")
    int deleteStaff(Integer staffUserId, Integer businessId);

    @Select("select bui.user_id AS id,p.email,p.photo,p.phone,p.first_name,p.last_name,p.gender from business_user_identity bui Join person p on p.id=bui.user_id where business_id=#{businessId}")
    List<Person> getAllStaff(Integer businessId);

    @Select("select id, creator, is_deleted, updater, activated, username, status from users where username=#{username}")
    @Results({
        @Result(column = "id", property = "id"),
        @Result(column = "creator", property = "creator"),
        @Result(column = "is_deleted", property = "isDeleted"),
        @Result(column = "updater", property = "updater"),
        @Result(column = "activated", property = "activated"),
        @Result(column = "password", property = "password"),
        @Result(column = "username", property = "username"),
        @Result(column = "status", property = "status"),
        // 关联查询用户权限列表
        @Result(column = "id", property = "authorities",
                many = @Many(select = "com.elm.mapper.UserAuthorityMapper.getAuthorityById"))
    })
    User getUserByName(String username);

    @Update("update users set password=#{password} where username=#{username}")
    int updatePassword(User user);

    @Update("update users set status=#{status} where id=#{targetUserId}")
    int updateStatus(int targetUserId, String status);

    @Select("select business_id from business_user_identity where user_id=#{staffId}")
    List<Integer> getStaffWorkByStaffId(int staffId);
}